package com.example.lcpharmacy.service;

import com.example.lcpharmacy.entity.GoodsInfoEntity;
import com.example.lcpharmacy.entity.resBean.ReturnGoodsInfo;
import com.example.lcpharmacy.mapper.GoodsInfoDao;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsInfoService {
    @Autowired
    GoodsInfoDao goodsInfoDao;

    /**
     * PageHelper ==>  limit pageNum,pageSize
     */
    public PageInfo<GoodsInfoEntity> goodsInfoPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<GoodsInfoEntity> goodsInfoEntities = goodsInfoDao.selectGoods();
        return new PageInfo<GoodsInfoEntity>(goodsInfoEntities);
    }


    /**
     * 根据商品编号和商品名称 分页查询
     */
    public PageInfo<GoodsInfoEntity> queryByCodeAName(String goodsCode, String goodsName, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<GoodsInfoEntity> goodsInfoEntities = goodsInfoDao.selectByNameAndCode(goodsCode, goodsName);
        return new PageInfo<GoodsInfoEntity>(goodsInfoEntities);
    }

    /**
     * 根据商品编号 分页查询
     */
    public PageInfo<GoodsInfoEntity> queryByGoodsCode(String goodsCode, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<GoodsInfoEntity> goodsInfoEntities = goodsInfoDao.selectByGoodCode(goodsCode);
        return new PageInfo<GoodsInfoEntity>(goodsInfoEntities);
    }

    /**
     * 根据商品名称 分页查询
     */
    public PageInfo<GoodsInfoEntity> queryByGoodsName(String goodsName, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<GoodsInfoEntity> goodsInfoEntities = goodsInfoDao.selectByGoodName(goodsName);
        return new PageInfo<GoodsInfoEntity>(goodsInfoEntities);
    }

    /**
     * 更新商品信息
     *
     * @return
     */
    public String updateGoodsInfo(GoodsInfoEntity goodsInfoEntity) {
        List<GoodsInfoEntity> checkItems1 = goodsInfoDao.checkUpdateCode(goodsInfoEntity);
        List<GoodsInfoEntity> checkItems2 = goodsInfoDao.checkUpdateName(goodsInfoEntity);
        if (goodsInfoEntity.getId() != 0) {
            if (!(checkItems1.isEmpty())) {
                return "商品编号已存在！";
            } else if (!(checkItems2.isEmpty())) {
                return "商品名称已存在！";
            } else {
                goodsInfoDao.updateCodeById(goodsInfoEntity);
                return "商品信息更新成功！";
            }
        } else {
            if (!(checkItems1.isEmpty())) {
                return "商品编号已存在！";
            } else if (!(checkItems2.isEmpty())) {
                return "商品名称已存在！";
            } else {
                goodsInfoDao.insertGoodsInfo(goodsInfoEntity);
                return "商品信息添加成功！";
            }
        }
    }

    /**
     * 根据商品编号 查询单个商品
     * 将图片url拆分
     */
    public ReturnGoodsInfo selectOne(GoodsInfoEntity goodsInfoEntity) {
        GoodsInfoEntity goodsInfo = goodsInfoDao.selectOneGood(goodsInfoEntity);
        ReturnGoodsInfo returnGoodsInfo = new ReturnGoodsInfo();

        //商品基本信息装载
        returnGoodsInfo.setGoodsInfoEntity(goodsInfo);

        //解析商品主图字段中的url
        String[] goodsPics = goodsInfo.getGoodsPicture().split(",");
        List<String> goodsP1s = new ArrayList<>();
        if (goodsPics.length != 0) {
            for (String goodsPic : goodsPics) {
                goodsP1s.add(goodsPic);
            }
        }
        returnGoodsInfo.setGoodsPicture(goodsP1s);

        //解析商品详情缩略图字段中的url
        String[] goodsPics2 = goodsInfo.getGoodsPictures().split(",");
        List<String> goodsP2s = new ArrayList<>();
        if (goodsPics2.length != 0) {
            for (String goodsPic : goodsPics2) {
                goodsP2s.add(goodsPic);
            }
        }
        returnGoodsInfo.setGoodsPictures(goodsP2s);

        //解析商品详情高清图字段中的url
        String[] goodsJpgs = goodsInfo.getGoodsJpgs().split(",");
        List<String> goodsP3s = new ArrayList<>();
        if (goodsJpgs.length != 0) {
            for (String goodsPic : goodsJpgs) {
                goodsP3s.add(goodsPic);
            }
            returnGoodsInfo.setGoodsJpgs(goodsP3s);
        }

        return returnGoodsInfo;
    }
}
